package swissmap;

import swissmap.coordinates.CoConvert;
import swissmap.coordinates.SwissCoordinate;


public class Origin {
	
	private String res;
	private int xTile_bern;
	private int yTile_bern;

	private int pixx;	//(0,0) = lower left corner
	private int pixy;	
	private double coPerPixel;
	
	public Origin(String res, int xTile, int yTile, int pixx, int pixy, double coPerPixel) {
		super();
		this.res = res;
		this.xTile_bern = xTile;
		this.yTile_bern = yTile;
		this.pixx = pixx;
		this.pixy = pixy;
		this.coPerPixel=coPerPixel;
	}

	/**
	 * get lower left Coordinate of tile with 600 000, 200 000 calibration point (Bern)
	 * @return
	 */
	private SwissCoordinate getLowerLeftCoBern(){
		SwissCoordinate c;

			c=new SwissCoordinate((int)(600000-(pixx*coPerPixel)), (int)(200000-(pixy*coPerPixel)));
		
		return c;
	}
	
	/**
	 * get Coordinate of Tile(x,y)=(0,0)
	 * @return
	 */
	private SwissCoordinate getLowerLeftCo00(){
		SwissCoordinate c;
		c=new SwissCoordinate((int)(600000-(((256*xTile_bern)+pixx)*coPerPixel)), 
							  (int)(200000-(((256*yTile_bern)+pixy)*coPerPixel)));
		
		return c;
	}
	
	
	/**
	 * 
	 * @param xTile
	 * @param yTile
	 * @param corner 1, upper left, 2upper right, 3 lower left, 4 lower right
	 * @return coordinate of Tile x,y origin 0,0 in lower left corner
	 */
	public SwissCoordinate getImgTileCo(int xTile, int yTile, int corner){
		SwissCoordinate tmp,c;
		tmp=getLowerLeftCo00();
		if(corner==1){
			c=new SwissCoordinate((int)(tmp.getLon()+(256 * xTile   *coPerPixel))	,
								  (int)(tmp.getLat()+(256 *(yTile+1)*coPerPixel)-1));
		}else if(corner==2){
			c=new SwissCoordinate((int)(tmp.getLon()+(256 *(xTile+1/**/)*coPerPixel)-1)	,(int)(tmp.getLat()+(256*(yTile+1)*coPerPixel)-1));
		}else if(corner==3){
			c=new SwissCoordinate((int)(tmp.getLon()+(256 * xTile        *coPerPixel))	,(int)(tmp.getLat()+(256*yTile*coPerPixel)));
		}else{
			c=new SwissCoordinate((int)(tmp.getLon()+(256 *(xTile+1/**/)*coPerPixel))-1 ,(int)(tmp.getLat()+(256*yTile*coPerPixel)));
		}
		return c;
	}
	
	
	
	public SwissCoordinate getScaleUpperLeftCo(boolean min){
		SwissCoordinate tmp,c;
		tmp=getLowerLeftCoBern();
		
		if(min){
			c=new SwissCoordinate((int)(tmp.getLon()-(256*(xTile_bern-Constants.dimx_start.get(res))*coPerPixel)),(int)(tmp.getLat()+(256*(Constants.dimy_end.get(res)-yTile_bern+1)*coPerPixel)));
		}else{
			c=new SwissCoordinate((int)(tmp.getLon()-(256*xTile_bern*coPerPixel)),(int)(tmp.getLat()+(256*(Constants.dimy_end_max.get(res)-yTile_bern+1)*coPerPixel)));
		}
		return c;
	}

	
	public SwissCoordinate getScaleUpperRightCo(boolean min){
		SwissCoordinate tmp,c;
		tmp=getLowerLeftCoBern();
		
//		System.out.println(tmp.getLon());
//		System.out.println(tmp.getLon()-(256*xTile*coPerPixel));
//		System.out.println(tmp.getLat());
//		System.out.println(tmp.getLat()-(256*yTile*coPerPixel));
		if(min){
			c=new SwissCoordinate((int)(tmp.getLon()+(256*(Constants.dimx_end_max.get(res)-xTile_bern+1)*coPerPixel)),(int)(tmp.getLat()+(256*(Constants.dimy_end.get(res)-yTile_bern+1)*coPerPixel)));
		}else{
			c=new SwissCoordinate((int)(tmp.getLon()+(256*(Constants.dimx_end_max.get(res)-xTile_bern+1)*coPerPixel)),(int)(tmp.getLat()+(256*(Constants.dimy_end_max.get(res)-yTile_bern+1)*coPerPixel)));
		}
		return c;
	}
	
	/**
	 * Lower left Coordinate
	 * @return
	 */
	public SwissCoordinate getScaleOriginCo(boolean min){
		SwissCoordinate tmp,c;
		tmp=getLowerLeftCoBern();
		
//		System.out.println(tmp.getLon());
//		System.out.println(tmp.getLon()-(256*xTile*coPerPixel));
//		System.out.println(tmp.getLat());
//		System.out.println(tmp.getLat()-(256*yTile*coPerPixel));
		if(min){
			c=new SwissCoordinate((int)(tmp.getLon()-(256*(xTile_bern-Constants.dimx_start.get(res))*coPerPixel)),(int)(tmp.getLat()-(256*(yTile_bern-Constants.dimy_start.get(res))*coPerPixel)));
		}else{
			c=new SwissCoordinate((int)(tmp.getLon()-(256*xTile_bern*coPerPixel)),(int)(tmp.getLat()-(256*yTile_bern*coPerPixel)));
		}
		return c;
	}
	
	public SwissCoordinate getScaleLowerRightCo(boolean min){
		SwissCoordinate tmp,c;
		tmp=getLowerLeftCoBern();
		
//		System.out.println(tmp.getLon());
//		System.out.println(tmp.getLon()-(256*xTile*coPerPixel));
//		System.out.println(tmp.getLat());
//		System.out.println(tmp.getLat()-(256*yTile*coPerPixel));
		if(min){
			c=new SwissCoordinate((int)(tmp.getLon()+(256*(Constants.dimx_end_max.get(res)-xTile_bern+1)*coPerPixel)),(int)(tmp.getLat()-(256*(yTile_bern-Constants.dimy_start.get(res))*coPerPixel)));
		}else{
			c=new SwissCoordinate((int)(tmp.getLon()+(256*(Constants.dimx_end_max.get(res)-xTile_bern+1)*coPerPixel)),(int)(tmp.getLat()-(256*yTile_bern*coPerPixel)));
		}
		return c;
	}
	
	
	public static void main(String[] args) {

		for(String s:Constants.origin.keySet())	{
			//String s="100k";
			SwissCoordinate c1,c2,c3,c4;
			boolean min=false;//true;
			c1=Constants.origin.get(s).getScaleUpperLeftCo(min);
			c2=Constants.origin.get(s).getScaleUpperRightCo(min);
			c3=Constants.origin.get(s).getScaleOriginCo(min);
			c4=Constants.origin.get(s).getScaleLowerRightCo(min);
//			System.out.println(s+"     1 "+c1);
//			System.out.println(s+"     2 "+c2);
			System.out.println(s+"     3 "+c3);
//			System.out.println(s+"     4 "+c4);
//			System.out.println(s+"     1 "+CoConvert.SwissgridToElipsoidCo(c1));
//			System.out.println(s+"     2 "+CoConvert.SwissgridToElipsoidCo(c2));
//			System.out.println(s+"     3 "+CoConvert.SwissgridToElipsoidCo(c3));
//			System.out.println(s+"     4 "+CoConvert.SwissgridToElipsoidCo(c4)+"\n");
			
			
		}
		System.out.println();
		double d= 8192;
		for(String s:Constants.dimx_end.keySet()){
			System.out.println(s+"      "+Constants.dimx_end_max.get(s)*256+"  "+Constants.dimy_end_max.get(s)*256);
			System.out.println("            "+Constants.dimx_end_max.get(s)*256/d+"  "+Constants.dimy_end_max.get(s)*256/d);
		}
	}

	
}
